﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using AesApplication.Models;
using WebMatrix.WebData;

namespace AesApplication.Controllers
{
    [Authorize]
    public class StaffingExpertController : Controller
    {

        private AesContext db = new AesContext();
        //
        // GET: /StaffingExpert/
        public ActionResult Index()
        {
            return View(BuildIndex());
        }


        [HttpPost]
        public ActionResult Index(StaffingExpertIndexViewModel model)
        {
            if (model.SelectedItem == 0 && model.Questionnaires == null)
            {
                //Add a questionnaire
                Questionnaire addition = new Questionnaire
                {
                    Name = "New Questionnaire",

                    Questions = new List<Question> {
                            new Question {
                                Number=1,
                                Type=QuestionType.MultipleChoice,
                                Text="New Question Text{A}Yes{B}No{C}",
                            },
                    },
                };

                db.Questionnaires.Add(addition);
            }
            else
            {
                if (model.Questionnaires != null)
                {
                    //Change in title
                    Questionnaire result = model.Questionnaires[0];
                    db.Questionnaires.Find(result.ID).Name = model.Questionnaires[0].Name;
                    
                }
                else
                {
                    //Find questionnaire to be removed
                    //since selected is position based we need a list of all un attached questionnaires 
                    Questionnaire qnaire = db.UnattachedQuestionnaires().ToList<Questionnaire>()[model.SelectedItem - 1];

                    //Removing the Questionnaire alone does not remove the questions as well. 
                    List<Question> questions = qnaire.Questions.ToList<Question>();
                    foreach (Question question in questions)
                    {
                        db.Questions.Remove(question);
                    }
                    db.Questionnaires.Remove(qnaire);
                }
            }
            db.SaveChanges();
            return View(BuildIndex());
        }

        private StaffingExpertIndexViewModel BuildIndex()
        {
            StaffingExpertIndexViewModel model = new StaffingExpertIndexViewModel();
            model.Items = new List<SelectListItem>();
            model.Questionnaires = new List<Questionnaire>();
            model.SelectedItem = 1;

            int i = 1;
            foreach (Questionnaire qnaire in db.UnattachedQuestionnaires())
            {
                model.Questionnaires.Add(qnaire);
                model.Items.Add(new SelectListItem { Value = i.ToString(), Text = qnaire.Name });
                i++;
            }
            return model;
        }


        public ActionResult QuestionCreator(int QuestionnaireId = 1, int QuestionNum = 1)
        {

            Questionnaire qnaire = db.Questionnaires.Find(QuestionnaireId);
            List<Question> results = qnaire.Questions.ToList<Question>();
            Question q = results[QuestionNum-1];
   

            if (q == null)
            {
                return HttpNotFound();
            }
            
            StaffingExpertQuestionCreatorModel model = new StaffingExpertQuestionCreatorModel();
            model.Question = q;
            model.QuestionTypes = new List<SelectListItem>();
           
            int count = 0;
            foreach (QuestionType qt in Enum.GetValues(typeof(QuestionType)))
            {
                 model.QuestionTypes.Add(new SelectListItem { Value = count.ToString(), Text = qt.ToString() });
                 count++;
            }
            
            switch (q.Type) 
            {
                case QuestionType.OneLine:
                    model.SelectedType = 0;
                    break;
                case QuestionType.Essay:
                    model.SelectedType = 1;
                    break;
                case QuestionType.Numeric:
                    model.SelectedType = 2;
                    break;
                case QuestionType.TrueFalse:
                    model.SelectedType = 3;
                    break;
                case QuestionType.MultipleChoice:
                   
                    model.MultipleChoiceOptions = q.GetMultipleChoiceOptions();
                    model.Question.Text = model.Question.GetDisplayText();
                    model.SelectedType = 4;
                    break;
                default:
                    model.SelectedType = 0;
                    break;
            }

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult QuestionCreator(StaffingExpertQuestionCreatorModel result)
        {
            if (ModelState.IsValid)
            {
                Questionnaire qnaire = db.Questionnaires.Find(result.Question.Questionnaire.ID);
                
                //look up the questionnaire, modify the question via the questionnaire

                foreach (Question i in qnaire.Questions) 
                {
                    if (i.ID == result.Question.ID) 
                    {
                        
                        db.Questions.Find(result.Question.ID).ExpectedAnswer = result.Question.ExpectedAnswer;
                        switch (result.SelectedType) 
                        {
                            case 0:
                                db.Questions.Find(result.Question.ID).Type = QuestionType.OneLine;
                                db.Questions.Find(result.Question.ID).Text = result.Question.Text;
                                break;
                              
                            case 1:
                                db.Questions.Find(result.Question.ID).Type = QuestionType.Essay;
                                db.Questions.Find(result.Question.ID).Text = result.Question.Text;
                                break;
                            case 2:
                                db.Questions.Find(result.Question.ID).Type = QuestionType.Numeric;
                                db.Questions.Find(result.Question.ID).Text = result.Question.Text;
                                break;
                            case 3:
                                db.Questions.Find(result.Question.ID).Type = QuestionType.TrueFalse;
                                db.Questions.Find(result.Question.ID).Text = result.Question.Text;
                                break;
                                
                            case 4:
                                db.Questions.Find(result.Question.ID).Type = QuestionType.MultipleChoice;
                                result.MultipleChoiceOptions = db.Questions.Find(result.Question.ID).GetMultipleChoiceOptions();

                                String options = "";
                                foreach(var item in result.MultipleChoiceOptions)
                                {
                                    options = options + "{" + item.Name + "}" + item.Text;
                                }
                                db.Questions.Find(result.Question.ID).Text = result.Question.Text+options;
                                break;

                        }
                        
                    }
                }

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(result);

        }
	}
}